import { formatDate } from "@/utils/utils";

export default class Item {
    title: string;
    dates: string[] = [];
    valueField: string;
    showFields: string[] = [];
    rawData: { list: Array<DataItem>, Real: Array<DataItem>, Target: Array<DataItem> } = {
        list: [],//误差值
        Real: [],//真实值
        Target: [],//目标值
    };
    hashList: hashItem = new hashItem();
    ref: any = {};
    constructor(title: string, valueField: string, showFields: string[] = []) {
        this.title = title;
        this.valueField = valueField;
        this.showFields = showFields;
    }

    setHashList(startTime: Date, endTime: Date, span = 2000) {
        // debugger;
        let that = this;
        let latest = {} as any;//{position:distance}
        if (this.showFields.includes("error")) {
            this.rawData.list.reduce((prev, cur, i) => {
                //判断cur.dateTime点位范围是否有值
                let index = getIndexByDatetime(startTime, cur.dataTime);

                //补位
                appendEmpty(index - that.hashList["error"].length);

                // 添加当前数据项
                let sp = countSpan(that.dates[index], cur.dataTime);
                if (typeof latest[index] == "undefined" || latest[index] > sp) {
                    Object.keys(new DataItem()).forEach(field => {
                        this.hashList[field][index] = cur[field] || "";
                    });
                    latest[index] = sp;
                }

                return cur;
            }, {
                dataTime: formatDate(startTime, "yyyy-MM-dd HH:mm:ss.000")
            });

            // 最后一个数据之后补位
            let lastIndex = getIndexByDatetime(startTime, endTime.getTime());
            appendEmpty(lastIndex + 1 - that.hashList["error"].length);
        }
        if (this.showFields.includes("real")) {
            this.rawData["Real"].reduce((prev, cur, i) => {
                //判断cur.dateTime点位范围是否有值
                let index = getIndexByDatetime(startTime, cur.dataTime);

                //补位
                appendEmpty(index - that.hashList["real"].length, ["real"]);

                // 添加当前数据项
                let sp = countSpan(that.dates[index], cur.dataTime);
                if (typeof latest[index] == "undefined" || latest[index] > sp) {
                    this.hashList["real"][index] = cur["error"] || "";
                    latest[index] = sp;
                }

                return cur;
            }, {
                dataTime: formatDate(startTime, "yyyy-MM-dd HH:mm:ss.000")
            });

            // 最后一个数据之后补位
            let lastIndex = getIndexByDatetime(startTime, endTime.getTime());
            appendEmpty(lastIndex + 1 - that.hashList["real"].length, ["real"]);
        }
        if (this.showFields.includes("target")) {
            this.rawData["Target"].reduce((prev, cur, i) => {
                //判断cur.dateTime点位范围是否有值
                let index = getIndexByDatetime(startTime, cur.dataTime);

                //补位
                appendEmpty(index - that.hashList["target"].length, ["target"]);

                // 添加当前数据项
                let sp = countSpan(that.dates[index], cur.dataTime);
                if (typeof latest[index] == "undefined" || latest[index] > sp) {
                    this.hashList["target"][index] = cur["error"] || "";
                    latest[index] = sp;
                }

                return cur;
            }, {
                dataTime: formatDate(startTime, "yyyy-MM-dd HH:mm:ss.000")
            });

            // 最后一个数据之后补位
            let lastIndex = getIndexByDatetime(startTime, endTime.getTime());
            appendEmpty(lastIndex + 1 - that.hashList["target"].length, ["target"]);
        }

        function countSpan(raw, cur) {
            return Math.abs(new Date(cur).getTime() - new Date(raw).getTime()) % span;
        }
        //判断cur.dateTime点位范围是否有值
        function getIndexByDatetime(rootTime, curTime) {
            let root = new Date(rootTime).getTime();
            let cur = new Date(curTime).getTime();
            let index = Math.floor((cur - root) / span);
            index += ((cur - root) % span) > (span / 2) ? 1 : 0;
            return index;
        }

        // 追加空数据
        function appendEmpty(num: number, fields = ["error", "maximum", "minimum", "average", "variance",]) {
            if (num <= 0) return;
            fields.forEach(field => {
                that.hashList[field].push(...new Array(num).fill(""));
            });
        }
    }

    reset() {
        this.rawData = {
            list: [],
            Real: [],
            Target: [],
        };
        this.hashList = new hashItem();
    }
}

export class hashItem {
    constructor() {
    }
    dataTime: string[] = [];    //时间点
    error?: number[] = [];       //误差
    maximum?: number[] = [];     //最大值
    minimum?: number[] = [];     //最小值
    average?: number[] = [];     //平均值
    variance?: number[] = [];    //方差
    real?: number[] = [];       //真实值
    target?: number[] = [];     //目标值
}

class DataItem {
    constructor() {
    }
    dataTime: string = "";
    error: number = 0;
    maximum?: number = 0;
    minimum?: number = 0;
    average?: number = 0;
    variance?: number = 0;
}
